package com.android.namecard.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.android.namecard.R;
import com.android.namecard.model.NamecardDBHelper;

public class StandarNamecardActivity extends Activity implements OnGestureListener {
	private ViewFlipper flipper;
    private ImageView modelImage;
	private GestureDetector detector;
	
	private TextView name;
	private TextView career;
	private TextView company;
	private TextView address;
	private TextView phone;
	private TextView email;
	private TextView qq;
	private TextView msn;
	private TextView website;
	private TextView business;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.standar_namecard);
        
        this.getTextView();
        
        detector = new GestureDetector(this);
		flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper01);
		modelImage = (ImageView)findViewById(R.id.standar_model_image);
		modelImage.setImageResource(R.drawable.model1_front);
		
		Button previewBut = (Button)findViewById(R.id.standar_preview);
		Button submitBut = (Button)findViewById(R.id.standar_submit);
		//预览按钮
		previewBut.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
                preview();
			}
		});
		//提交按钮
		submitBut.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
    }
    
    private void getTextView(){
    	name = (TextView)findViewById(R.id.standar_name);
    	career = (TextView)findViewById(R.id.standar_career);
    	company = (TextView)findViewById(R.id.standar_company);
    	address = (TextView)findViewById(R.id.standar_address);
    	phone = (TextView)findViewById(R.id.standar_phone);
    	email = (TextView)findViewById(R.id.standar_email);
    	qq = (TextView)findViewById(R.id.standar_qq);
    	website = (TextView)findViewById(R.id.standar_website);
    	msn = (TextView)findViewById(R.id.standar_msn);
    	business = (TextView)findViewById(R.id.standar_business);
    }
    
    private void preview(){
		Intent intent = new Intent(StandarNamecardActivity.this, PreviewActivity.class);
		Bundle bundle = new Bundle();
		bundle.putString(NamecardDBHelper.NAME, name.getText().toString());
		bundle.putString(NamecardDBHelper.CAREER, career.getText().toString());
		bundle.putString(NamecardDBHelper.COMPANY, company.getText().toString());
		bundle.putString(NamecardDBHelper.ADDRESS, address.getText().toString());
		bundle.putString(NamecardDBHelper.PHONE, phone.getText().toString());
		bundle.putString(NamecardDBHelper.EMAIL, email.getText().toString());
		bundle.putString(NamecardDBHelper.QQ, qq.getText().toString());
		bundle.putString(NamecardDBHelper.WEBSITE, website.getText().toString());
		bundle.putString(NamecardDBHelper.MSN, msn.getText().toString());
		bundle.putString(NamecardDBHelper.BUSINESS, business.getText().toString());
		intent.putExtras(bundle);
		startActivity(intent);
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    	
    }
    
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		if (e1.getX() - e2.getX() > 100) {
			this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
			this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
			this.flipper.showNext();
			return true;
		} else if (e1.getX() - e2.getX() < -100) {
			this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
			this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
			this.flipper.showPrevious();
			return true;
		}
		return false;
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return this.detector.onTouchEvent(event);
	}
}
